package org.linlinjava.litemall.db.service.distribution;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.DisMemberInfoMapper;
import org.linlinjava.litemall.db.dao.DisProfitParamMapper;
import org.linlinjava.litemall.db.dao.DisProfitRecordMapper;
import org.linlinjava.litemall.db.dao.distribution.DisProfitRecordDao;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.vo.DisProfitRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 交易Dao
 *
 * @author huangpu
 * @Date 2018-04-06 12:19:23
 */
@Service
@Transactional
public class DisProfitRecordService {


    @Autowired
    DisProfitRecordDao disProfitRecordDao;

    @Autowired
    DisProfitParamMapper disProfiParamMapper;

    @Autowired
    DisMemberInfoMapper disMemberInfoMapper;

    @Autowired
    DisProfitRecordMapper disProfitRecordMapper;


    public List<DisProfitRecord> querySelective(String username, String getUserId, String setUserId, Integer page, Integer limit) {
        DisProfitRecordExample example = new DisProfitRecordExample();
        DisProfitRecordExample.Criteria criteria = example.createCriteria();


        if (!StringUtils.isEmpty(getUserId)) {
            criteria.andDisGetUserIdEqualTo(getUserId);
        }
        if (!StringUtils.isEmpty(setUserId)) {
            criteria.andDisSetUserIdEqualTo(setUserId);
        }
        criteria.andIsDeleteEqualTo("N");

        PageHelper.startPage(page, limit);
        return disProfitRecordMapper.selectByExample(example);

    }

    public int countSeletive(String username, String setUserId, String getUserId, Integer page, Integer limit) {
        DisProfitRecordExample example = new DisProfitRecordExample();
        DisProfitRecordExample.Criteria criteria = example.createCriteria();


        if (!StringUtils.isEmpty(getUserId)) {
            criteria.andDisGetUserIdEqualTo(getUserId);
        }
        if (!StringUtils.isEmpty(setUserId)) {
            criteria.andDisSetUserIdEqualTo(setUserId);
        }
        criteria.andIsDeleteEqualTo("N");

        PageHelper.startPage(page, limit);
        return (int)disProfitRecordMapper.countByExample(example);
    }

    public Integer countByUserId(Integer userId) {
        DisProfitRecordExample example = new DisProfitRecordExample();
        DisProfitRecordExample.Criteria criteria = example.createCriteria();
        if (userId!=null) {
            criteria.andDisGetUserIdEqualTo(userId.toString());
        }
        criteria.andIsDeleteEqualTo("N");

        return (int)disProfitRecordMapper.countByExample(example);
    }

    public List<DisProfitRecord> getOrderList(Integer userId, Integer page, String status) {
        return disProfitRecordDao.getOrderList(userId, (page-1)*5, status);
    }

    public void update(DisProfitRecord record) {
        disProfitRecordMapper.updateByPrimaryKey(record);
    }

    public void insert(DisProfitRecord record) {
        disProfitRecordMapper.insert(record);
    }
}
